let sale=require("./datbase.js");
function easy(inputs){
    for(let i=0;i<inputs.length;i++)
        {
            if (inputs[i].length!=10)
                {
                    for(let j=0;j<parseInt(inputs[i].substr(11,1));j++)
                        {
                            inputs.splice(i+1,0,inputs[i].substr(0,10));
                        }
                    inputs.splice(i,1);
                }
        }
        let outputs=inputs;
        return outputs;
    }
function count(inputs){
    let outputs=[];
    for(let i=0;i<inputs.length;){
        let sum=0;
        for(let j=i;j<inputs.length;j++){
            if(inputs[i]==inputs[j])
                sum+=1;
        }
        outputs.push({barcode:inputs[i],count:sum});
        i+=sum;
    }
    return outputs;
}
function long(inputs){
    let all=sale.loadAllItems();//这里有问题
    for(let i=0;i<inputs.length;i++){
        for(let j=0;j<all.length;j++){
            if(inputs[i]['barcode']==all[j]['barcode'])
                {
                    inputs[i]["name"]=all[j]["name"];
                    inputs[i]["unit"]=all[j]["unit"];
                    inputs[i]["price"]=all[j]["price"];
                }
        }
    }
    let outputs=inputs;
    return outputs;
}
function less(inputs){
    let all=sale.loadPromotions();
    for(let i=0;i<inputs.length;i++){
        inputs[i].sale=0;
    }
    for(let i=0;i<inputs.length;i++){
        for(let j=0;j<all[0]['barcodes'].length;j++){
            if(all[0]['barcodes'][j]==inputs[i]['barcode'])
                {
                    let less=parseInt(inputs[i]['count']/3);
                    inputs[i]['count']-=less;
                    inputs[i]['sale']=less;
                }
        }

    }
    let outputs=inputs;
    return outputs;
}
function sum(inputs){
    let sum=0;
    let sale=0;
    let collection=[];
    for(let i=0;i<inputs.length;i++){
        sum+=inputs[i]['count']*inputs[i]['price'];
        sale+=inputs[i]['sale']*inputs[i]['price'];
    }
    collection.push()

}
exports.main = function main(inputs) {
    let a=easy(inputs);
    let b=count(a);
    let c=long(b);
    let collection=less(c);
    let sum=0;
    let sale=0;
    for(let i=0;i<collection.length;i++){
        sum+=collection[i]['count']*collection[i]['price'];
        sale+=collection[i]['sale']*collection[i]['price'];
    }
    let result="***<没钱赚商店>购物清单***\n";
    for(let i=0;i<collection.length;i++){
        result+=`名称：${collection[i]['name']}，数量：${collection[i]['count']+collection[i]['sale']}${collection[i]['unit']}，单价：${collection[i]['price'].toFixed(2)}(元)，小计：${(collection[i]['price']*collection[i]['count']).toFixed(2)}(元)\n`
    }
    result+='----------------------\n';
    result+='挥泪赠送商品：\n';
    for(let i=0;i<collection.length;i++){
        if(collection[i]['sale']!=0)
            {
                result+=`名称：${collection[i]['name']}，数量：${collection[i]['sale']}${collection[i]['unit']}\n`;
            }
    }
    result+='----------------------\n';
    result+=`总计：${sum.toFixed(2)}(元)\n`;
    result+=`节省：${sale.toFixed(2)}(元)\n`;
    result+='**********************';
    console.log(result);
    
};
exports.less=less;
exports.long=long;
exports.count=count;
exports.easy=easy;
